import { defineStore } from 'pinia'
import { ref, computed } from 'vue'
import '../types' // 导入全局类型声明

export const useSettingsStore = defineStore('settings', () => {
  // 主题设置
  const theme = ref('light')
  const themeColor = ref('#0c3d92') // 默认主题色
  
  // 编辑器设置
  const editorMode = ref('wysiwyg')
  const fontSize = ref(14) // 单位: px
  const lineHeight = ref(1.6) // 行高: 1.6
  const showLineNumbers = ref(true)
  
  // 自动保存设置
  const autoSave = ref(true)
  const autoSaveInterval = ref(5) // 单位: 秒
  
  // 同步设置
  const syncEnabled = ref(false)
  const syncProvider = ref('')
  
  // 判断是否为暗黑模式
  const isDarkMode = computed(() => theme.value === 'dark')
  
  // 获取主题
  const getTheme = async () => {
    try {
      const theme = await window.elec.settings.get('theme')
      return theme || 'light'
    } catch (error) {
      console.error('获取主题设置失败', error)
      return 'light'
    }
  }
  
  // 设置主题
  const setTheme = async (value: string) => {
    try {
      theme.value = value
      await window.elec.settings.set('theme', value)
      return true
    } catch (error) {
      console.error('保存主题设置失败', error)
      return false
    }
  }

  // 获取主题颜色
  const getThemeColor = async () => {
    try {
      const color = await window.elec.settings.get('themeColor')
      return color || '#0c3d92'
    } catch (error) {
      console.error('获取主题颜色设置失败', error)
      return '#0c3d92'
    }
  }
  
  // 设置主题颜色
  const setThemeColor = async (value: string) => {
    try {
      themeColor.value = value
      await window.elec.settings.set('themeColor', value)
      return true
    } catch (error) {
      console.error('保存主题颜色设置失败', error)
      return false
    }
  }
  
  // 获取编辑器模式
  const getEditorMode = async () => {
    try {
      const mode = await window.elec.settings.get('editorMode')
      return mode || 'wysiwyg'
    } catch (error) {
      console.error('获取编辑器模式设置失败', error)
      return 'wysiwyg'
    }
  }
  
  // 设置编辑器模式
  const setEditorMode = async (value: string) => {
    try {
      editorMode.value = value
      await window.elec.settings.set('editorMode', value)
      return true
    } catch (error) {
      console.error('保存编辑器模式设置失败', error)
      return false
    }
  }
  
  // 获取自动保存设置
  const getAutoSave = async () => {
    try {
      const value = await window.elec.settings.get('autoSave')
      return value === 'true'
    } catch (error) {
      console.error('获取自动保存设置失败', error)
      return true
    }
  }
  
  // 设置自动保存
  const setAutoSave = async (value: boolean) => {
    try {
      autoSave.value = value
      await window.elec.settings.set('autoSave', value.toString())
      return true
    } catch (error) {
      console.error('保存自动保存设置失败', error)
      return false
    }
  }
  
  // 获取自动保存间隔
  const getAutoSaveInterval = async () => {
    try {
      const value = await window.elec.settings.get('autoSaveInterval')
      return parseInt(value) || 5
    } catch (error) {
      console.error('获取自动保存间隔设置失败', error)
      return 5
    }
  }
  
  // 设置自动保存间隔
  const setAutoSaveInterval = async (value: number) => {
    try {
      autoSaveInterval.value = value
      await window.elec.settings.set('autoSaveInterval', value.toString())
      return true
    } catch (error) {
      console.error('保存自动保存间隔设置失败', error)
      return false
    }
  }
  
  // 获取行号显示设置
  const getShowLineNumbers = async () => {
    try {
      const value = await window.elec.settings.get('showLineNumbers')
      return value === 'true'
    } catch (error) {
      console.error('获取行号显示设置失败', error)
      return true
    }
  }
  
  // 设置行号显示
  const setShowLineNumbers = async (value: boolean) => {
    try {
      showLineNumbers.value = value
      await window.elec.settings.set('showLineNumbers', value.toString())
      return true
    } catch (error) {
      console.error('保存行号显示设置失败', error)
      return false
    }
  }
  
  // 获取同步设置
  const getSyncEnabled = async () => {
    try {
      const value = await window.elec.settings.get('syncEnabled')
      return value === 'true'
    } catch (error) {
      console.error('获取同步设置失败', error)
      return false
    }
  }
  
  // 设置同步
  const setSyncEnabled = async (value: boolean) => {
    try {
      syncEnabled.value = value
      await window.elec.settings.set('syncEnabled', value.toString())
      return true
    } catch (error) {
      console.error('保存同步设置失败', error)
      return false
    }
  }
  
  // 初始化设置
  const initSettings = async () => {
    // 加载主题设置
    const themeValue = await getTheme()
    theme.value = themeValue
    
    // 加载主题颜色
    const colorValue = await getThemeColor()
    themeColor.value = colorValue
    
    // 加载编辑器模式
    const editorModeValue = await getEditorMode()
    editorMode.value = editorModeValue
    
    // 加载自动保存设置
    const autoSaveValue = await getAutoSave()
    autoSave.value = autoSaveValue
    
    // 加载自动保存间隔
    const autoSaveIntervalValue = await getAutoSaveInterval()
    autoSaveInterval.value = autoSaveIntervalValue
    
    // 加载行号显示设置
    const showLineNumbersValue = await getShowLineNumbers()
    showLineNumbers.value = showLineNumbersValue
    
    // 加载同步设置
    const syncEnabledValue = await getSyncEnabled()
    syncEnabled.value = syncEnabledValue
  }
  
  return {
    // 状态
    theme,
    themeColor,
    editorMode,
    fontSize,
    lineHeight,
    autoSave,
    autoSaveInterval,
    showLineNumbers,
    syncEnabled,
    syncProvider,
    isDarkMode,
    
    // 方法
    getTheme,
    setTheme,
    getThemeColor,
    setThemeColor,
    getEditorMode,
    setEditorMode,
    getAutoSave,
    setAutoSave,
    getAutoSaveInterval,
    setAutoSaveInterval,
    getShowLineNumbers,
    setShowLineNumbers,
    getSyncEnabled,
    setSyncEnabled,
    initSettings
  }
}) 